package org.openrbac.rbac.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.openrbac.rbac.constant.CommonConstant;
import org.openrbac.rbac.controller.base.BaseController;
import org.openrbac.rbac.entity.Auth;
import org.openrbac.rbac.service.AuthService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author bapie
 */
@RestController
@RequestMapping("/auth")
public class AuthController extends BaseController<AuthService, Auth> {

    private static final Logger log = LoggerFactory.getLogger(AuthController.class);

    @GetMapping("/page")
    public IPage<Auth> getPageAuths(@RequestParam(value = "current", defaultValue = "1") long current,
                                    @RequestParam(value = "size",defaultValue = "10") long size,
                                    @RequestParam(value = "sort", defaultValue = "asc") String sort,
                                    @RequestParam(value = "name",defaultValue = "") String name,
                                    @RequestParam(value = "isHidden",defaultValue = "") Boolean isHidden,
                                    @RequestParam(value = "createTime",defaultValue = "") String createTime) {
        IPage<Auth> page = service.getPageAuths(current, size, sort, name, isHidden, createTime);
        return page;
    }

    @DeleteMapping("/del")
    public boolean delAuth(@RequestBody List<Long> ids) {
        System.out.println(ids);
        return service.delAuth(ids);
    }

    @PostMapping("/add")
    public Boolean add(@RequestBody Auth auth) {
        return service.addAuth(auth);
    }

    @Override
    public Boolean edit(@RequestBody Auth auth) {
        return service.updateById(auth);
    }


    @GetMapping("/get2")
    public List<Auth> get(HttpServletRequest request) {
        String username = request.getHeader(CommonConstant.USERNAME);
        return service.findAuths(username);
    }

    @GetMapping("/getRouters")
    public List<Auth> getRoutrs(@RequestParam("web") String web) {
        web.replace("\"","");
        return service.getRouters(web);
    }
}
